export default class WTLocalDB {
    constructor(name) {
        this.LS = null
        this.name = name
        this.checkLS()
        this.init(name)
    }

    checkLS() {
        if (window && window.localStorage) {
            this.LS = window.localStorage
            // console.log('localStorage is there?')
        } else {
            console.log('localStorage is there?')
        }
    }

    init(name) {
        if (this.LS) {
            if (this.LS[name]) {
                this.data = JSON.parse(this.LS[name])
            } else {
                this.data = {}
            }
        }
    }

    set(key, data) {
        this.data[key] = data
        if (this.LS) {
            this.LS[this.name] = JSON.stringify(this.data)
        }
    }

    delete(key) {
        if (this.data[key]) {
            delete this.data[key];
            if (this.LS) {
                this.LS[this.name] = JSON.stringify(this.data)
            }
            return true;
        }
        return false;
    }

    get(key) {
        if (this.data[key]) {
            return this.data[key]
        }
        return false
    }
}

//保存數據到H5緩存
export const SetCacheToLS = (key, data) => {
    var _db = new WTLocalDB("wt-db");
    _db.set(key, data)
}
//從H5緩存讀取數據
export const GetCacheToLS = (key) => {
    var _db = new WTLocalDB("wt-db");
    return _db.get(key)
}
//從H5緩存讀取數據
export const DelCacheToLS = (key) => {
    var _db = new WTLocalDB("wt-db");
    _db.delete(key)
}
 